package com.soushangxing.comfyuibackendv2.controller

import com.soushangxing.comfyuibackendv2.bean.ResponseType
import com.soushangxing.comfyuibackendv2.service.ModelService
import com.soushangxing.comfyuibackendv2.service.UserService
import jakarta.servlet.http.HttpServletRequest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.*

@Controller
@ResponseBody
@RequestMapping("model")
class ModelController {
    @Autowired
    lateinit var modelService: ModelService

    @Autowired
    lateinit var userService: UserService

    @PostMapping("/add")
    fun add(
        @RequestParam name: String,
        @RequestParam typeCode: String,
        @RequestParam description: String,
        @RequestParam prompt: String,
        @RequestParam status: String,
        request: HttpServletRequest
    ): ResponseType {
        if (!userService.isAdmin(request)) {
            return ResponseType(null, 2, "非管理员禁止调用本接口")
        }
        return try {
            val model = modelService.add(name, typeCode, description, prompt, status)
            ResponseType(model, 0, "添加成功")
        } catch (e: Exception) {
            ResponseType(null, 1, "添加失败: ${e.message}")
        }
    }

    @PostMapping("/update")
    fun update(
        @RequestParam name: String,
        @RequestParam typeCode: String,
        @RequestParam description: String,
        @RequestParam prompt: String,
        @RequestParam status: String,
        request: HttpServletRequest
    ): ResponseType {
        if (!userService.isAdmin(request)) {
            return ResponseType(null, 2, "非管理员禁止调用本接口")
        }
        return try {
            val updatedModel = modelService.update(name, typeCode, description, prompt, status)
            if (updatedModel.isPresent) {
                ResponseType(updatedModel.get(), 0, "更新成功")
            } else {
                ResponseType(null, 1, "更新失败: 模型未找到")
            }
        } catch (e: Exception) {
            ResponseType(null, 1, "更新失败: ${e.message}")
        }
    }

    @PostMapping("/delete")
    fun delete(@RequestParam name: String, request: HttpServletRequest): ResponseType {
        if (!userService.isAdmin(request)) {
            return ResponseType(null, 2, "非管理员禁止调用本接口")
        }
        return try {
            val deleted = modelService.delete(name)
            if (deleted) {
                ResponseType(null, 0, "删除成功")
            } else {
                ResponseType(null, 1, "删除失败: 模型未找到")
            }
        } catch (e: Exception) {
            ResponseType(null, 1, "删除失败: ${e.message}")
        }
    }

    @GetMapping("/status")
    fun findByStatus(@RequestParam status: String, request: HttpServletRequest): ResponseType {
        if (!userService.isAdmin(request)) {
            return ResponseType(null, 2, "非管理员禁止调用本接口")
        }
        return try {
            val models = modelService.findByStatus(status)
            ResponseType(models, 0, "查询成功")
        } catch (e: Exception) {
            ResponseType(null, 1, "查询失败: ${e.message}")
        }
    }
}
